// pages/login/phone-login.js
const app = getApp()
const util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginBtn:false,
    phone:'',
    send:false,
    time:60,
    sendBtn:false,
    verCode:'',
    time:60,
    phoneFocus:false,
    checkFlag:false,
    del:false
  },
  clearNum: function (e) {
    if (e.detail.value.length > 0) {
      this.setData({
        del: true
      })
    }
  },
  cancel: function (e) {
    this.setData({
      phone: '',
      checkFlag: true,
      del: false
    })
    // console.log(this.data.phone)
  },
  phone: function (e) {
    if (e.detail.value.length > 0) {
      this.setData({
        del: true
      })
    }
    if (e.detail.value.length == '11') {
      this.setData({
        loginBtn: true,
        phone: e.detail.value,
        send:true
      })
    }
  },
  blur: function(e){
    setTimeout(() => {
      if (this.data.checkFlag) {
        this.setData({
          checkFlag: false
        })
        return
      }
      this.setData({
        phone: e.detail.value,
      })
      // console.log(this.data.phone+'dddddddddd')
      if (e.detail.value.length == '11') {
        this.setData({
          loginBtn: true,
          send: true
        })
      }
    },500)
  },
  formSubmit: function(e){
    if(!this.wxValidate.checkForm(e)){
      wx.showToast({
        title: this.wxValidate.errorList[0].msg,
        image: '/images/register/cuowu@2x.png',
        duration: 2000
      }) 
      return     
    }
    this.login()

  },
  sendCode:function(e){
      if(!this.data.send){
        return false
      }
      if(this.data.phone == ''){
        wx.showToast({
          title: '请填写手机号',
          image: '/images/register/cuowu@2x.png',
          duration: 2000
        })
        return false
      }
      let check = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(this.data.phone)
      if (this.data.send && !check) {
        wx.showToast({
          title: '手机号错误',
          image: '/images/register/cuowu@2x.png',
          duration: 2000
        })
        return false
      }else{  
        wx.request({
          url: app.globalData.baseUrl + '/WXloginByPhone',
          data: {
            phone: this.data.phone,
            verCode: '',
            code: app.globalData.code
          },
          method: 'post',
          header: {
            'content-type': 'application/json'
          },
          success:  (res) => {
            // console.log(res.data)
            if (res.data.code == '0') {
              this.setData({
                sendBtn: true
              })
              this.setData({
                time: '60'
              })
              let time1 = setInterval(() => {
                if (this.data.time <= 0) {
                  clearInterval(time1)
                  this.setData({
                    sendBtn: false
                  })
                } else {
                  this.setData({
                    time: --this.data.time
                  })
                }
              }, 1000)
            } else if (res.data.code == '1') {
              wx.showToast({
                title: res.data.data.Exception,
                image: '/images/register/cuowu@2x.png',
                duration: 2000
              })
            }  else {
              wx.showToast({
                title: res.data.message,
                image: '/images/register/cuowu@2x.png',
                duration: 2000
              })
            }
          }
        })
      }
      
    // }
  },
  code:function(e){
    this.setData({
      verCode: e.detail.value
    })
    
  },
  login:function(e){
    wx.request({
      url: app.globalData.baseUrl + '/WXloginByPhone',
      data: {
        phone: this.data.phone,
        verCode: this.data.verCode,
        code: app.globalData.code
      },
      method: 'post',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res.data)
        if (res.data.code == '0') {
          wx.setStorageSync('userInfo', res.data.data.user)
          wx.setStorageSync('session', res.data.data.user.session_key)
          wx.setStorageSync('openid', res.data.data.user.openid)
          util.shareFn()
        } else if (res.data.code == '1') {
          wx.navigateTo({
            url: '../../unenter/unenter',
          })
        } else {
          wx.showToast({
            title: res.data.message,
            image: '/images/register/cuowu@2x.png',
            duration: 2000
          })
        }
      }
    })
  },
  uncode:function(e){
    wx.showModal({
      content: '请拨打客服电话：400-996-3626',
      confirmText:'拨打',
      confirmColor:'#2A8EFF',
      success: function (res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: '400-996-3626'
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  loginByPhone:function(){
    wx.navigateTo({
      url: '../accountLogin/accountLogin?phone='+this.data.phone,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.wxValidate = app.wxValidate(
      {
        mobile: {
          required: true,
          tel: true,
        },
        code: {
          required: true,
          minlength: 6
        }
      }, {
        mobile: {
          required: '请填写手机号'
          // tel: 'dsfsdfsdf'
        },
        code: {
          required: '请填写验证码',
          minlength: '验证码错误'
        }
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log(this.data.phone)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})